# CMake minimum version
cmake_minimum_required (VERSION 3.10)

set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Project Infomation
project( overlays C )

set(CMAKE_ALWAYS_DELETE_TEMPORARY_FILES OFF)

enable_language(ASM)

string(TIMESTAMP COMPILE_TIME %Y-%m-%dT%H:%M:%S )
set(build_time ${COMPILE_TIME})

# Reset output path
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

set (CMAKE_C_FLAGS "-std=c99 -Wl,-Map,gcc.map " )

# STDLIB
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)

# Set include path
include_directories ( ./Core/Inc)
include_directories ( ./Overlays)
include_directories ( ./Drivers/CMSIS/Device/ST/STM32F4xx/Include )
include_directories ( ./Drivers/CMSIS/Include )
include_directories ( ./Drivers/STM32F4xx_HAL_Driver/Inc)
include_directories ( ./Drivers/STM32F4xx_HAL_Driver/Inc/Legacy)

# The need build source path and build all files
aux_source_directory ( ./Core/Src DIR_USER_SRCS)
aux_source_directory ( ./Overlays DIR_USER_SRCS)
aux_source_directory ( ./Drivers/STM32F4xx_HAL_Driver/Src DIR_STM32F4xx_HAL_Driver_SRCS )

configure_file(
	"$ENV{PROJECT_DIR}/VersionConfig.h.in"
	"$ENV{PROJECT_DIR}/VersionConfig.h"
	)

# CC ARL
set(CMAKE_C_COMPILER $ENV{CC} )
set(CMAKE_CXX_COMPILER $ENV{CXX} )
set(CMAKE_C_AR $ENV{AR})
set(CMAKE_C_LD $ENV{LD})

# CFLAGS
set (CMAKE_C_FLAGS "-g -O1 -mthumb -fno-builtin -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -u _scanf_float -Wall -std=gnu99 -ffunction-sections -fdata-sections -fomit-frame-pointer -mabi=aapcs -fno-unroll-loops -ftree-vectorize" CACHE INTERNAL "c compiler flags")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D USE_HAL_DRIVER -D STM32F407xx")

# ASMFLAGS
set (CMAKE_ASM_FLAGS "-g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -u _scanf_float -x assembler-with-cpp" CACHE INTERNAL "asm compiler flags")
 
# LDFLAGS
set (CMAKE_EXE_LINKER_FLAGS "--specs=nosys.specs -g -Wl,--gc-sections -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -u _printf_float -u _scanf_float -mabi=aapcs -Wl,-Map,gcc.map" CACHE INTERNAL "executable linker flags")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${CMAKE_CURRENT_SOURCE_DIR}/STM32F407VGTX_FLASH.ld")
	
add_executable (${CMAKE_PROJECT_NAME}.elf ${DIR_USER_SRCS} ${DIR_STM32F4xx_HAL_Driver_SRCS} ./Core/Startup/startup_stm32f407vgtx.s)

# Generate the binary file
add_custom_target (${CMAKE_PROJECT_NAME}.bin ALL arm-none-eabi-objcopy -Obinary "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf" "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.bin" DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf)

# Generate the hex file
add_custom_target (${CMAKE_PROJECT_NAME}.hex ALL arm-none-eabi-objcopy -Oihex "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf" "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.hex" DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf)

# Echo the size Infomation
add_custom_target (size ALL arm-none-eabi-size "${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf" DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.elf)

# Make flash to the board by st-link
add_custom_target (flash COMMAND st-flash write ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_PROJECT_NAME}.bin 0x8000000)

# Make clean-all
add_custom_target (clean-all-core COMMAND rm -rf ${CMAKE_BINARY_DIR}/*)
